package com.ease.framework.workbench
{
	import com.ease.util.collections.HashMap;
	import com.ease.util.collections.Map;
	import com.ease.framework.workbench.editors.EditorInput;
	import com.ease.framework.workbench.parts.IEditorFactory;
	import com.ease.framework.workbench.parts.IEditorPart;
	import com.ease.framework.workbench.viewparts.ViewPartDefinition;
	import com.ease.kernel.Kernel;

	import flash.events.EventDispatcher;
	import flash.utils.getQualifiedClassName;

	import mx.collections.ArrayCollection;
	import mx.core.Container;

	public class Workbench extends EventDispatcher
	{
		//记录当前正在操作的Editor
		private var _currentEditor:IEditorPart;

		private var _isDirty:Boolean = false;

		private var _editors:ArrayCollection = new ArrayCollection;
		private var _viewers:Map = new HashMap();

		private var _mainFrame:main;

		private static var _instance:Workbench;

		public function get mainFrame():main{
			return _mainFrame;
		}

		public function set mainFrame(value:main):void{
			_mainFrame = value;
		}

		public function get isDirty():Boolean{
			return _isDirty;
		}

		public function set isDirty(value:Boolean):void{
			_isDirty = value;
		}

		public function get editors():ArrayCollection{
			return _editors;
		}

		public function get currentEditor():IEditorPart{
			return _currentEditor;
		}

		public function set currentEditor(editor:IEditorPart):void{
			this._currentEditor = editor;

			if(!editors.contains(editor)){
				editors.addItem(editor);
			}
		}

		public function get viewers():Map{
			return _viewers;
		}

		public function Workbench(access:Privace){
			if(access)
				if(_instance == null){
					_instance = this;

					viewers.put(WorkbenchLocation.TOP,new ArrayCollection());
					viewers.put(WorkbenchLocation.LEFT,new ArrayCollection());
					viewers.put(WorkbenchLocation.RIGHT,new ArrayCollection());
					viewers.put(WorkbenchLocation.BOTTOM,new ArrayCollection());
				}
		}

		public static function get instance():Workbench{
			if(_instance == null) _instance = new Workbench(new Privace);
			return _instance;
		}

		public function openEditor(input:EditorInput):void{
			if(currentEditor == null){
				createEditor(input);
			}

			currentEditor.editorInput = input;
		}

		public function createEditor(input:EditorInput):IEditorPart{
			var editorFactory:IEditorFactory = Kernel.getInstance(getQualifiedClassName(IEditorFactory)).getService(input.type) as IEditorFactory;

			var editor:IEditorPart = editorFactory.createEditor();

			currentEditor = editor;

			var editorControl:Container = editor.createPartControl();
			editorControl.percentHeight = 100;
			editorControl.percentWidth = 100;

			mainFrame.editBox.addChild(editorControl);

			return editor;
		}

		public function refreshViews():void{

			var factories:ArrayCollection = Kernel.getInstance(getQualifiedClassName(IPerspectiveFactory)).getServices("com.ease.framework.workbench.Workbench");

			for each(var factory:IPerspectiveFactory in factories){
				if(factory.views && factory.views.length>0){
					for each(var view:ViewPartDefinition in factory.views){
						if(viewers.getObject(view.location))
							viewers.getObject(view.location).addItem(view);
					}
				}
			}

		}

	}
}
class Privace{}